<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function jsonp({ url, params, callback }) {
        return new Promise((resolve, reject) => {
          let script = document.createElement('script')
          window[callback] = function(data) {
            resolve(data)
            document.body.removeChild(script)
          }
          params = { ...params, callback } // wd=b&callback=show
          let arrs = []
          for (let key in params) {
            arrs.push(`${key}=${params[key]}`)
          }
          script.src = `${url}?${arrs.join('&')}`
          document.body.appendChild(script)
        })
      }
      jsonp({
        url: 'http://localhost:3000/say',
        params: { wd: 'Iloveyou' },
        callback: 'show'
      }).then(data => {
        console.log(data)
      })
    </script>
  </body>
</html>
